package com.ktgj.flight.common.linkstar.util;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import sun.misc.BASE64Encoder;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

/**
 * @author wyj
 * @create 2021-10-27 17:31
 */
public class HttpHelper {

    public static String sendByHttps(String username, String pwd, String url, String requestXml) {

        String responseBody = "";

        try (CloseableHttpClient httpClient = createSSLClientDefault()) {
            HttpPost httpPost = new HttpPost(url);

            // 请求参数的数据压缩
            HttpEntity requestEntity = EntityBuilder.create().setText(requestXml)
                    .setContentType(ContentType.create(ContentType.APPLICATION_XHTML_XML.getMimeType(), Consts.UTF_8))
                    .gzipCompress().build();

            //base64 加密用户名密码
            BASE64Encoder decoder = new BASE64Encoder();
            String userInfo = decoder.encode((username + ":" + pwd).getBytes("UTF-8"));

            //设置 header 信息,Basic 后面有一个空格
            httpPost.addHeader("Authorization", "Basic " + userInfo);
            //用户可根据需要在请求 HTTPS Header 中传入 EchoToken 参数，标识本次请求，便于排查问题
            httpPost.addHeader("EchoToken", "asdfsa123231");
            httpPost.addHeader("Content-Encoding", "gzip");
            httpPost.addHeader("Accept-Encoding", "gzip");
            httpPost.setEntity(requestEntity);

            try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
                HttpEntity httpEntity = httpResponse.getEntity();
                StatusLine status = httpResponse.getStatusLine();
                int statusCode = status.getStatusCode();
//                System.out.println("statusCode =\n" + statusCode);
                if (httpEntity != null) {
                    responseBody = EntityUtils.toString(httpEntity, "UTF-8");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return responseBody;
    }

    public static CloseableHttpClient createSSLClientDefault() throws Exception {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (a, b) -> true).build();
        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext
                , new String[]{"TLSv1.2", "TLSv1", "TLSv1.1"}
                , new String[]{"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
                "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA"}
                , hostnameVerifier);
        return HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    }

}

